//
//  ZYVisitorView.swift
//  CZ29WeiboZYH
//
//  Created by 沐颜澈 on 16/6/25.
//  Copyright © 2016年 muyanche. All rights reserved.
//

import UIKit
import SnapKit

class ZYVisitorView: UIView {
    
    //中间按钮的点击闭包
    var closure:(() -> ())?
    
    
    
   override init(frame: CGRect) {
        super.init(frame: frame)
    
        setupUI()
    
    
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置UI
    private func setupUI() {
        
        // 设置一个背景颜色
        backgroundColor =  RBG(240, green: 240 , blue: 240)
        
        //1.添加控件
        addSubview(feedImageView)
        addSubview(maskImageView)
        addSubview(IconImageView)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(registerBtn)
        
        //2.添加约束
        feedImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
            
        }
        
        //遮罩的约束
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        //房子的约束
        IconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        //文字
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(224)
            make.height.equalTo(36)
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.centerX.equalTo(maskImageView)
            
            
        }
        
        //登录按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(15)
            
        }
        
        //注册按钮
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(15)
            
        }
        
    }
    
    // MARK: -  设置动画
    func setupAnim() {
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        //动画时间
        ani.duration = 9
        //旋转角度(toValue)
        ani.toValue = M_PI * 2
        
        //动画次数(MAXFLOAT最大次数
        ani.repeatCount = MAXFLOAT
        
        //切换的时候默认动画完成
        ani.removedOnCompletion = false
        
        //添加动画
        feedImageView.layer.addAnimation(ani, forKey: nil)
    }
    
    //MARK: - 外界调用的方法,主页的房子和label的图标
    func setupVisitorView(imagname: String?, title: String?) {
        //如果有值就代表不是主页
        if let img = imagname, let titl = title {
            IconImageView.image = UIImage(named: img)
            messageLabel.text = titl
            //将圆圈隐藏起
            feedImageView.hidden = true
        
        } else {
            
            setupAnim()
        
        }
        
    }
    
    //MARK: - 给登录注册按钮添加点击事件,执行闭包,弹出控制器
    @objc private func btnClcik() {
        
        print("中间登录注册按钮点击啦")
        
        //
        closure?()
        
        
    }
    
    //MARK: - 懒加载控件
    //中间的圆圈
    private lazy var feedImageView: UIImageView = UIImageView(imagName: "visitordiscover_feed_image_smallicon")
    //添加遮罩
    private lazy var maskImageView: UIImageView  = UIImageView(imagName: "visitordiscover_feed_mask_smallicon")
    
    //添加中间的房子图片
    private lazy var IconImageView: UIImageView = UIImageView(imagName: "visitordiscover_feed_image_house")
    
    //设置文字
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.textColor = ThemeColor
        label.textAlignment = .Center
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜~~得得得"
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(13)
        
        return label
        
    }()
    
    //设置登录的按钮
    lazy var loginButton: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: "btnClcik", forControlEvents: UIControlEvents.TouchUpInside)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(13)
        
        return btn
        
        
    }()
    
    //设置注册的按钮
    lazy var registerBtn: UIButton = {
        
        let btn = UIButton()
        btn.addTarget(self, action: "btnClcik", forControlEvents: UIControlEvents.TouchUpInside)
               btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
         btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(13)
        
        return btn
    }()
    
    
    
    
}




















